<?php
namespace app\appoint\model;
use app\BaseModel;
use think\facade\Db;



class QuestionList extends BaseModel
{
    //定义表名
    protected $name = 'longbing_card_question_list';


    protected $append = [

        'next_id'

    ];


    /**
     * @author chenniang
     * @DataTime: 2021-01-26 16:30
     * @功能说明:获取下一道题
     */
    public function getNextIdAttr($value,$data){

        if(!empty($data['id'])&&!empty($data['top'])&&!empty($data['uniacid'])&&!empty($data['q_id'])){

            $dis = [

                'uniacid' => $data['uniacid'],

                'q_id'    => $data['q_id'],

                'text_id' => 0

            ];

            $id = $this->where($dis)->where('top','<',$data['top'])->order('top desc,id desc')->value('id');

            return $id;

        }

    }

    /**
     * @param $dis
     * @param $page
     * @功能说明:问卷列表
     * @author chenniang
     * @DataTime: 2021-01-25 15:56
     */
    public function dataList($dis,$page){

        $data = $this->where($dis)->order('top desc')->paginate($page)->toArray();

        return $data;
    }

    /**
     * @param $dis
     * @功能说明:问卷下拉框
     * @author chenniang
     * @DataTime: 2021-01-25 15:56
     */
    public function dataSelect($dis){

        $data = $this->where($dis)->order('top desc,id desc')->select()->toArray();

        if(!empty($data)){

            $text_model = new QuestionText();

            foreach ($data as &$v){
                //查询每道题的内容
                $v['data_text'] = $text_model->textInfo($v);

            }
        }

        return $data;
    }

    /**
     * @param $data
     * @功能说明:添加
     * @author chenniang
     * @DataTime: 2021-01-25 15:57
     */
    public function dataAdd($data){

        $text = $data['data_text'];

        unset($data['data_text']);

        Db::startTrans();

        $res = $this->insert($data);

        $id  = $this->getLastInsID();

        $this->updateSome($id,$text,$data['uniacid'],$data['type']);

        Db::commit();

        return $res;
    }



    /**
     * @author chenniang
     * @DataTime: 2021-01-25 18:10
     * @功能说明:添加内容详情
     */
    public function updateSome($q_id,$text,$uniacid,$type=1){

        $text_model = new QuestionText();

        $text_model->where(['q_id'=>$q_id,'q_type'=>$type])->delete();

        if($type==1){
            //添加每个问题
            foreach ($text['que'] as $v){
                //增加子题目
                $insert = [

                    'uniacid' => $uniacid,

                    'q_id'    => $q_id,

                    'text'    => $v['text'],
                    //1是问题 2是答案
                    'type'    => 1,

                    'q_type'  => 1,

                ];

                $res = $text_model->dataAdd($insert);
                //获取问题的id
                $pid = $text_model->getLastInsID();
                //答案是个一维数组 所有的答案都有一个同意的问题 一一对应
                foreach ($text['ans'] as $value ){
                    //增加答案
                    $insert = [

                        'uniacid' => $uniacid,

                        'q_id'    => $q_id,

                        'text'    => $value['text'],
                        //1是问题 2是答案
                        'type'    => 2,

                        'q_type'  => 1,

                        'pid'     => $pid
                    ];

                    $res = $text_model->dataAdd($insert);

                    if($res != 1){

                        Db::rollback();

                    }

                }

            }
        }

        if($type==2){

            foreach ($text['ans'] as $value ){
                //增加答案
                $insert = [

                    'uniacid' => $uniacid,

                    'q_id'    => $q_id,

                    'text'    => $value['text'],
                    //1是问题 2是答案
                    'type'    => 2,

                    'q_type'  => 2,

                    'pid'     => $q_id,

                    'is_jump' => $value['is_jump'],

                    'jump_id' => $value['jump_id']
                ];

                $res = $text_model->dataAdd($insert);

                if($res != 1){

                    Db::rollback();

                }
            }
        }

        return true;
    }

    /**
     * @param $dis
     * @param $data
     * @功能说明:编辑
     * @author chenniang
     * @DataTime: 2021-01-25 15:57
     */
    public function dataUpdate($dis,$data){

        $text = $data['data_text'];

        unset($data['data_text']);

        Db::startTrans();

        $res = $this->where($dis)->update($data);

        $this->updateSome($data['id'],$text,$data['uniacid'],$data['type']);

        Db::commit();

        return $res;

    }

    /**
     * @param $dis
     * @功能说明:详情
     * @author chenniang
     * @DataTime: 2021-01-25 15:57
     */
    public function dataInfo($dis){

        $data = $this->where($dis)->find();

        return !empty($data)?$data->toArray():$data;

    }








}